package hottargui.config;

import java.util.Map;

import hottargui.framework.*;
import hottargui.standard.*;

public class AlphaNewGameState implements TarguiState {
	
	Game gameContext;
	
	public AlphaNewGameState(Game game)
	{
		this.gameContext = game;
	}
	
	public boolean buy(int count, Position deploy) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public State getState() {
		return State.newGame;
	}

	public boolean move(Position from, Position to, int count) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void newGame() throws IllegalActionException {
	    // Setup board
	    GameObjectServer objServer = gameContext.getGameObjectServer();
	    objServer.getBoardStrategy().setupBoard(objServer.getGameData());
	    
	    // Setup players
	    StandardPlayer redPlayer = new StandardPlayer(PlayerColor.Red, new Position(0,0));
	    StandardPlayer greenPlayer = new StandardPlayer(PlayerColor.Green, new Position(0,6));
	    StandardPlayer bluePlayer = new StandardPlayer(PlayerColor.Blue, new Position(6,0));
	    StandardPlayer yellowPlayer = new StandardPlayer(PlayerColor.Yellow, new Position(6,6));

	    // Set initial camels on settlement tiles
	    StandardTile settlement;
	    settlement = (StandardTile)gameContext.getTile(redPlayer.getSettlemenet());
	    settlement.changeUnitCount(10);
	    settlement = (StandardTile)gameContext.getTile(greenPlayer.getSettlemenet());
	    settlement.changeUnitCount(10);
	    settlement = (StandardTile)gameContext.getTile(bluePlayer.getSettlemenet());
	    settlement.changeUnitCount(10);
	    settlement = (StandardTile)gameContext.getTile(yellowPlayer.getSettlemenet());
	    settlement.changeUnitCount(10);

	    // Setup turnSequence
	    Map<PlayerColor,Player> players = objServer.getGameData().getPlayers();
	    players.put(redPlayer.getColor(), redPlayer);
	    players.put(greenPlayer.getColor(), greenPlayer);
	    players.put(bluePlayer.getColor(), bluePlayer);
	    players.put(yellowPlayer.getColor(), yellowPlayer);
	    

	    objServer.getGameData().setCurrentColor(turnCard());
	}

	public void rollDie() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public PlayerColor turnCard() throws IllegalActionException {
	    GameObjectServer objServer = gameContext.getGameObjectServer();	
	    objServer.getGameData().setCurrentColor(objServer.getTurnSequenceStrategy().getNextPlayer());
	    gameContext.setState(gameContext.getFactory().getMoveState(gameContext));
	    return objServer.getGameData().getCurrentColor();
	}

	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

}
